namespace SampleModel;

class DataObject implements \ArrayAccess, \Countable
{
	protected _data;
	protected _dirty;
	private   _property;

    public function initialize(array! data, array! property)
    {
        let this->_data = data;
        let this->_dirty = [];
        let this->_property = property;
    }

	public function offsetExists(var index) -> boolean
	{
		let index = strval(index);
		return isset this->_data[index];
	}

	public function offsetSet(var index, var value) -> void
	{
		let index = strval(index);
		if isset this->_property[index] {
			let this->_dirty[] = index;
		}
		let this->_data[index] = value;
	}

	public function offsetUnset(var index) -> void
	{
		let index = strval(index);

		unset(this->_data[index]);
	}

	public function offsetGet(var index) -> string
	{
		let index = strval(index);

		return this->_data[index];
	}

	public function count() -> int
	{
		return count(this->_data);
	}

	/**
	 * Magic method to assign values to the the model
	 *
	 * @param string property
	 * @param mixed value
	 */
	public function __set(var property, value)
	{
		let property = strval(property);
		if isset this->_property[property] {
			let this->_dirty[] = property;
		}
		let this->_data[property] = value;
	}

	public function __get(string! property)
	{
		let property = strval(property);

		/**
		 * Check if the property has getters
		 */
		var method;
		let method = "get" . camelize(property);

		if method_exists(this, method) {
			return this->{method}();
		}
		if isset this->_data[property]{
			return this->_data[property];
		}
		return null;
	}

	/**
	 * Magic method to check if a property is a valid relation
	 */
	public function __isset(string! property) -> boolean
	{
		let property = strval(property);

		return isset this->_data[property];
	}

    public function toArray() -> array
    {
        return this->_data;
    }
}
